//
//  ViewController.m
//  考试
//
//  Created by 王成龙 on 16/6/13.
//  Copyright © 2016年 wcl. All rights reserved.
//

#import "ViewController.h"
@interface PhoneData : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *phone;

@end

@implementation PhoneData

@end


@interface ViewController ()
{
    NSMutableArray * aabb;

}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"谢鹏涛电话簿";
    UITableView * tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain ];
    tableview.dataSource = self;
    [self.view addSubview:tableview];
    
    NSBundle * bundle = [NSBundle mainBundle];
    NSURL * url = [bundle URLForResource:@"PIM00001" withExtension:@"vcf"];
    NSString * text = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    
    NSArray * arr = [text componentsSeparatedByString:@"\n"];
    PhoneData *item;
    NSMutableArray * tmp = [NSMutableArray new];
    for(NSString*p in arr)
    {
        if([p hasPrefix:@"BEGIN"])
        {
            item = [PhoneData new];
        }
        else if([p hasPrefix:@"END"])
        {
            [tmp addObject:item];
        }
        else if([p hasPrefix:@"TEL"])
        {
            
            NSArray *components = [p componentsSeparatedByString:@":"];
            NSString * app = components[1];
            app = [app stringByReplacingOccurrencesOfString:@"-" withString:@""];
            app = [app stringByRemovingPercentEncoding];

            item.phone=app;
        }
        else if ([p hasPrefix:@"FN;"])
        {
            NSArray *upperComponents = [p componentsSeparatedByString:@":"];
            NSString *name = upperComponents[1];
            name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            name = [name stringByRemovingPercentEncoding];
            
            
            item.name = name;
        }
        
        
        aabb=tmp;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // return 10;
    return aabb.count;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell*call=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    PhoneData *p = aabb[indexPath.row];
    call.textLabel.text = p.phone;
    call.detailTextLabel.text = p.name;
    //改颜色
    call.backgroundColor =[UIColor colorWithRed:20*indexPath.row/255.0 green:40*indexPath.row/9000.0 blue:60*indexPath.row/255.0 alpha:1];
    
    call.accessoryType =UITableViewCellAccessoryDisclosureIndicator;//后面箭头
    
    return call;
}

    


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
